﻿Imports EstoqueDAL
Imports EstoqueBLL
Public Class FornecedorUC
    Dim objBLL As New fornecedorbll

#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Try
            Dim forn As New FornecedorCAD
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("for_codigo") Is Nothing) Then
                    objBLL = New FornecedorBLL(Val(DGVDados.Rows(linha.Index).Cells("for_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.Codigo >= 0) Then
                forn.LBCodigo.Text = objBLL.Codigo
                forn.Label2.Visible = True
                forn.LBCodigo.Visible = True
                forn.LBTitulo.Text = objBLL.razao
                TSBAlterar.BackColor = Color.Gainsboro
                forn.ShowDialog()
                CBFiltro_SelectedIndexChanged(Nothing, Nothing)
            End If
            TSBAlterar.BackColor = Color.Transparent
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub
#End Region

#Region "Carrega grid"
    Sub CarregaGrid()
        DGVDados.DataSource = objBLL.buscar("", 9)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Cadastrar"
    Private Sub TSBNovo_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        Try
            Dim forn As New FornecedorCAD
            TSBCadastrar.BackColor = Color.Gainsboro
            forn.ShowDialog()
            CBFiltro_SelectedIndexChanged(Nothing, Nothing)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBCadastrar.BackColor = Color.Transparent
        End Try

    End Sub
#End Region

#Region "ComboBox Filtro"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            CBStatus.Visible = True
            CBStatus.SelectedIndex = 0
            CBStatus_SelectedIndexChanged(Nothing, Nothing)
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            CarregaGrid()
        End If
    End Sub
#End Region

#Region "ComboboxStatus Selectedindex"
    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        Dim sta As Integer = 0
        If (CBStatus.Text = "Ativo") Then sta = 1
        If (CBStatus.Text = "Inativo") Then sta = 2
        DGVDados.DataSource = objBLL.buscar(sta, 5)
        DGVDados.Refresh()
    End Sub
#End Region

#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
       Try
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("for_codigo") Is Nothing) Then
                    objBLL = New FornecedorBLL(Val(DGVDados.Rows(linha.Index).Cells("for_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.Codigo > 0) Then
                TSBExcluir.BackColor = Color.Gainsboro
                If (MessageBox.Show("Confirma a exclusão dor fornecedor: " & objBLL.razao & "?", "Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes) Then
                    objBLL.Status = 3
                    objBLL.InserirAlterar(objusuarios.Codigo)
                    CBFiltro_SelectedIndexChanged(Nothing, Nothing)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBExcluir.BackColor = Color.Transparent
        End Try

    End Sub
#End Region

#Region "Localizar"
    Private Sub TSBLocalizar_Click(sender As System.Object, e As System.EventArgs) Handles TSBBuscar.Click
        If PLocalizar.Visible = True Then
            PLocalizar.Visible = False
            TXTLocalizar.Text = ""
            TSBBuscar.BackColor = Color.Transparent
        Else
            PLocalizar.Visible = True
            TXTLocalizar.Text = ""
            TSBBuscar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "KeyDown Form"
    Private Sub FornecedorUC_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            TSBVoltar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "Load"
    Private Sub FornecedorUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CarregaGrid()
        PLocalizar.Visible = False
        'Determinar o começo o filtro como descrição
        CBStatus.SelectedIndex = 0
        CBFiltro.SelectedIndex = 0
        CBFiltro.Focus()
    End Sub
#End Region

#Region "Localizar no textbox"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 0)
            Case "Razão Social"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 1)
            Case "Nome Fantasia"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 2)
            Case "CNPJ"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 3)
            Case "Inscr. Estadual"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 4)
            Case "Status"
                DGVDados.DataSource = objBLL.buscar(1, 5)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 9)
        End If
        DGVDados.Refresh()
    End Sub

#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBVoltar.Click
        statustabestoque = "N"
        Dispose()
    End Sub
#End Region

#Region "Keypress Localizar"
    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress, TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Or CBFiltro.SelectedIndex = 3 Or CBFiltro.SelectedIndex = 4 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, 1)
                DGVDados.Refresh()
            End If
        Else

        End If

    End Sub
#End Region

#Region "ComboboxFiltro SelectedIndex"
    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If (CBFiltro.Text = "Status") Then
            TXTLocalizar.Visible = False
            CBStatus.Visible = True
            CBStatus.SelectedIndex = 0
        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBStatus.Visible = False
        End If
        If TXTLocalizar.Text = "" Then
            CarregaGrid()
        End If
    End Sub
#End Region

#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region

#Region "DatagridView Alterar com duplo click"
    Private Sub DGVDado_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        TSBAlterar_Click(Nothing, Nothing)
    End Sub
#End Region

#Region "DatagridView Deletar"
    Private Sub DGVDado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles DGVDados.KeyDown
        If (e.KeyCode = Keys.Delete) Then
            TSBExcluir_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "DatagridView Alterar com enter"
    Private Sub DGVDado_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles DGVDados.KeyPress
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            TSBAlterar_Click(Nothing, Nothing)
        End If
    End Sub
#End Region

#Region "TSB relatorio"
    Private Sub TSBRelatorio_Click(sender As System.Object, e As System.EventArgs) Handles TSBRelatorio.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("for_codigo") Is Nothing) Then
                objBLL = New FornecedorBLL(Val(DGVDados.Rows(linha.Index).Cells("for_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.Codigo > 0) Then gerarelatrio("Fornecedor: " & objBLL.razao, "{Ofi_Fornecedor.for_codigo}='" & objBLL.Codigo & "'")
        End If
    End Sub
#End Region

#Region "Relatorio"
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosFRM
            Dim rpt As New FornecedorREL

            Try
                rpt.SetDatabaseLogon(usuarios, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer1.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer1.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region

End Class
